# -*- coding: utf-8 -*-
from os import path,mkdir,rmdir,listdir,remove




from os.path import exists as file_exists

#
def del_file( filename ):    
    if file_exists(filename):
        try:
            remove(filename)
        except:pass

        
def listdir(dirname='.', columns=40, truncate=0):
    """\
    sorted directory listing.

    dirname may be relative
    columns [=40] - min length of filenames
    truncate [=0] - max length of filenames
                    0 means don't truncate
    """
    import os, string

    dirname = os.path.abspath(dirname)
    dirlist = os.listdir(dirname)
    dirlist.sort()
    res = []

    isdir = os.path.isdir
    join  = os.path.join

    if truncate:
        # %%      -->  escape %
        # %-m.Ps  -->  left justification, m columns minimum length
        #              P columns precision (maximum length)
        format = '%%-%d.%ds %%s' % (columns, truncate)
    else:
        format = '%%-%ds %%s' % columns

    for filename in dirlist:
        fullname = join(dirname, filename)
        if isdir(fullname):
            typ = 'directory'
        else:
            # you may want to display the file size with
            # typ = '%9d' % os.path.getsize(fullname)
            # (right justification, 9 digits min length)
            typ = ''
        res.append(format % (filename, typ))
    return string.join(res, '\n')
